﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace Asplode
{
    public class Console
    {
        private static int MAX_CONSOLE_LINES = 10;
        private static int CONSOLE_LINE_DISAPPEAR_TIME_MS = 5000;

        private Color color;
        private SpriteFont font;
        private List<KeyValuePair<String, DateTime>> lines;
        private SpriteBatch spriteBatch;

        public Console(ContentManager content, SpriteBatch spriteBatch)
        {
            this.spriteBatch = spriteBatch;
            font = content.Load<SpriteFont>("Fonts\\ConsoleFont");
            lines = new List<KeyValuePair<String, DateTime>>();
            color = Color.White;
        }

        public void Print(String line)
        {
            // Remove old lines if necessary to make room for this one.
            while (lines.Count >= MAX_CONSOLE_LINES)
            {
                lines.RemoveAt(0);
            }

            lines.Add(new KeyValuePair<String, DateTime>(line, DateTime.Now));
        }

        public void Draw()
        {
            // Any lines older than this time will be removed.
            DateTime cutoff = DateTime.Now.AddMilliseconds(-CONSOLE_LINE_DISAPPEAR_TIME_MS);

            while (lines.Count > 0 && lines[0].Value < cutoff)
            {
                lines.RemoveAt(0);
            }

            int xPos = 20;
            int yPos = 20;
            Vector2 printPosition = new Vector2(xPos, yPos);
            foreach (KeyValuePair<String, DateTime> pair in lines)
            {
                spriteBatch.DrawString(font, pair.Key, printPosition, color);
                printPosition.Y += 20;
            }
        }
    }
}
